package com.emser.unc.server;

import java.util.ArrayList;

import com.emser.unc.client.service.ReportsService;
import com.emser.unc.serializable.NoSaleReportDTO;
import com.emser.unc.serializable.ProductSoldDTO;
import com.emser.unc.serializable.ResultDTO;
import com.emser.unc.serializable.SaleReportDTO;
import com.emser.unc.shared.exception.NotLoggedInException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ReportsServiceImpl extends RemoteServiceServlet implements ReportsService{
	private static final long serialVersionUID = 1L;
	
	@Override
	public ResultDTO<SaleReportDTO> getSaleReports(String cursorString, int range,String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			ResultDTO<SaleReportDTO> reports = AppLibReports.getSaleReports(cursorString, range,userAName);
			return reports;
		}
	}

	@Override
	public ResultDTO<NoSaleReportDTO> getSaleNoReports(String cursorString, int range,String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			ResultDTO<NoSaleReportDTO> reports = AppLibReports.getSaleNoReports(cursorString,range,userAName);
			return reports;
		}
	}
	@Override
	public Integer getSaleReportCount(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			int result = AppLibReports.getSaleReportCount(userAName);
			return result;
		}
	}
	@Override
	public Integer getNoSaleReportCount(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibReports.getNoSaleReportCount(userAName);
		}
	}

	@Override
	public ResultDTO<ProductSoldDTO> getProductsSold(String cursorString, int range,
			Long idReport, String userAName) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibReports.getProductsSold(cursorString,range,userAName, idReport);
		}
	}


	@Override
	public boolean deleteNoSaleReports(String userAName,
			ArrayList<Long> idReports) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibReports.deleteNoSaleReports(userAName, idReports);
		}
	}

	@Override
	public boolean deleteSaleReports(String userAName, ArrayList<Long> idReports)throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibReports.deleteSaleReports(userAName, idReports);
		}
	}

	@Override
	public Integer getProductsSoldsCount(String userAName, Long idReport)throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibReports.getProductsSoldQuantity(userAName, idReport);
		}
	}
	

}
